namespace eval cmd {}

proc cmd::add {comand wr} {
variable comands
set comands($comand) $wr
}

proc cmd::remove {comand} {
    variable $comand
	unset $comand
}


proc cmd::run {from type body} {
	set txt [split $body " "]
	set comand [string tolower [lindex $txt 0]]
	set body [join [lrange $txt 1 end]]
	debug cmd:run BODY "$body"
	
	variable comands
	set tmp "comands($comand)"
	debug cmd:run tmp "$tmp"
	if {![info exists [subst "$tmp"]]} {
		debug cmd:run "command $comand" "not exists"
		return
	}
	
	set wr [subst "\$$tmp"]
	debug cmd:run "WR=" "$wr"
	
	set code [catch { eval $wr [list $from $type $body]} state]
        debug comand ": $wr ->" "$state (code $code)"
	if {$code == 1} {
	    ::bgerror [format "Comand %s failed\nProcedure %s returned code\
			       %s\n\n%s" $comand $wr $code $state]
	}
}
hook::add message cmd::run 30